<template>
    <div>
        <!-- 搜索栏 -->
        <el-card id="search">
            <el-row>
                <el-col :span="18">
                    <el-input placeholder="名字" v-model="searchModel.name" clearable> </el-input>
                    <el-input placeholder="类别" v-model="searchModel.type" clearable> </el-input>
                    <el-button @click="getProductList" type="primary" icon="el-icon-search" round>查询</el-button>
                </el-col>
                <el-col :span="6" align="right">
                    <el-button @click="openEditUI(null)" type="primary" icon="el-icon-plus" circle></el-button>
                </el-col>
            </el-row>
        </el-card>
        <!-- 结果列表 -->
        <el-card>
            <el-table :data="productList" stripe style="width: 100%">
                <el-table-column label="#" width="80">
                    <template slot-scope="scope">
                        {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="type" label="类型编号" width="180">
                </el-table-column>
                
                <el-table-column prop="name" label="name">
                </el-table-column>
                <el-table-column prop="brand" label="品牌">
                </el-table-column>

                <el-table-column prop="series" label="系列">
                </el-table-column>
                <el-table-column prop="price" label="价格">
                </el-table-column>
                <el-table-column prop="description" label="详情">
                    <template slot-scope="scope">
                        <div class="ellipsis">{{ scope.row.description }}</div>
                    </template>
                </el-table-column>
                <el-table-column label="图片" width="120">
                    <template slot-scope="scope">
                        <img :src="scope.row.img" alt="Img" style="width: 50px; height: 50px; border-radius: 50%;" />
                    </template>
                </el-table-column>
                <el-table-column prop="stock" label="库存">
                </el-table-column>
                <el-table-column label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button @click="openEditUI(scope.row.id)" type="primary" icon="el-icon-edit" circle
                            size="mini"></el-button>
                        <el-button @click="deleteProduct(scope.row)" type="danger" icon="el-icon-delete" circle
                            size="mini"></el-button>
                        <el-button @click="goBack()" type="primary">返回</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="searchModel.pageNo" :page-sizes="[5, 10, 20, 50]" :page-size="searchModel.pageSize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 对话框 -->
        <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
            <el-form :model="productForm" ref="productFormRef" :rules="rules">
                <el-form-item prop="name" label="产品名称" :label-width="formLabelWidth">
                    <el-input v-model="productForm.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="brand" label="产品品牌" :label-width="formLabelWidth">
                    <el-input v-model="productForm.brand" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="产品类型" :label-width="formLabelWidth" class="custom-width">
                    <el-select v-model="productForm.type" placeholder="（1-手机，2-平板，3-电脑）" style="width: 400px;">
                        <el-option label="手机" value="0"></el-option>
                        <el-option label="平板" value="1"></el-option>
                        <el-option label="电脑" value="2"></el-option>
                    </el-select>
                </el-form-item>
               
                <el-form-item prop="series" label="产品系列" :label-width="formLabelWidth">
                    <el-input v-model="productForm.series" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item prop="price" label="产品价格" :label-width="formLabelWidth">
                    <el-input v-model="productForm.price" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="产品详情">
                    <el-input type="textarea" v-model="productForm.description"></el-input>
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveProduct">确 定</el-button>
            </div>
        </el-dialog>
  
    </div>
</template>
<script>
import productManage from '@/api/productManage'
export default {
    data() {

        return {
        
            formLabelWidth: '130px',
            productForm: {},
            dialogFormVisible: false,
            title: '',
            searchModel: {
                name: '',
                type: '',
                pageNo: 1,
                pageSize: 10
            },
            productList: [],
            total: 0,
            rules: {

            }
        }
    },
    methods: {
        goBack() {
            this.searchModel.name = ''; // 清空用户名搜索条件
            
            this.searchModel.pageNo = 1;

            // 返回操作逻辑
            this.getProductList();
        },
        deleteProduct(product) {
            this.$confirm(`您确定删除商品 ${product.productName} ？`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                product: 'warning'
            }).then(() => {
                productManage.deleteProductById(product.id).then(response => {
                    this.$message({
                        product: 'success',
                        message: response.message
                    });
                    this.dialogFormVisible = false;
                    this.getProductList();
                });

            }).catch(() => {
                this.$message({
                    product: 'info',
                    message: '已取消删除'
                });
            });
        },
        saveProduct() {
            // 触发表单验证
            this.$refs.productFormRef.validate((valid) => {
                if (valid) {
                   
                    // 提交保存请求
                    productManage.saveProduct(this.productForm).then(response => {
                        // 成功提示
                        this.$message({
                            message: response.message,
                            product: 'success'
                        });
                        // 关闭对话框
                        this.dialogFormVisible = false;
                        // 刷新表格数据
                        this.getProductList();
                    });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        clearForm() {
            this.productForm = {};
            this.$refs.productFormRef.clearValidate();
        },
        openEditUI(id) {
            if (id == null) {
                this.title = '新增分类';
            } else {
                this.title = '修改分类';
                productManage.getProductById(id).then(response => {
                    this.productForm = response.data;
                });
            }
            this.dialogFormVisible = true;
        },
        handleSizeChange(pageSize) {
            this.searchModel.pageSize = pageSize;
            this.getProductList();
        },
        handleCurrentChange(pageNo) {
            this.searchModel.pageNo = pageNo;
            this.getProductList();
        },
        getProductList() {
            productManage.getProductList(this.searchModel).then(response => {
                this.productList = response.data.rows;
                this.total = response.data.total;
            });
        }
    },
    created() {
        this.getProductList();
    }
};
</script>
    
<style>
#search .el-input {
    width: 200px;
    margin-right: 10px;
}

.el-dialog .el-input {
    width: 85%;
}

.ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.full-description {
    white-space: normal;
    word-wrap: break-word;
}
</style>